import {Ref} from "vue";
import {ElMessage} from "element-plus/es";
import {unbindWechat} from "@/api/orgStaff";
import {useHeader} from "@/compositions/main/useHeader";

export function useBindWechat() {
  const store = useStore()
  const isShowBindWechat:Ref<boolean> = ref(false)
  const currentUserInfo = ref({}) // userInfo
  const { exit } = useHeader()


  const showWechatBindDialog = (userInfo: any) => {
    currentUserInfo.value = userInfo
    isShowBindWechat.value = true
  }

  const _unbindWechat = async (userInfo: any) => {
    return new Promise(async (resolve, reject) => {
      const rs = await unbindWechat({
        userId: userInfo.id,
        app: 'merchant'
      })

      if (rs.code === 200) {
        ElMessage.success('解绑成功')
        resolve(rs)
      } else {
        reject(rs)
      }
    })
  }

  const unbindWechatConfirm = (userInfo: {id: number}) => {
    ElMessageBox.confirm('确定解绑微信？', '提示').then(() => {
      _unbindWechat(userInfo)
      exit()
    })
  }

  const isBindWechat = computed(() => {
    return store.state.user.bindWx
  })

  return {
    isShowBindWechat,
    currentUserInfo: currentUserInfo,
    showWechatBindDialog,
    unbindWechat: _unbindWechat,
    unbindWechatConfirm,
    isBindWechat,
  }
}
